{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "E:\\anaconda3\\lib\\importlib\\_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 192 from C header, got 216 from PyObject\n",
      "  return f(*args, **kwds)\n",
      "E:\\anaconda3\\lib\\importlib\\_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 192 from C header, got 216 from PyObject\n",
      "  return f(*args, **kwds)\n",
      "E:\\anaconda3\\lib\\importlib\\_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 192 from C header, got 216 from PyObject\n",
      "  return f(*args, **kwds)\n",
      "E:\\anaconda3\\lib\\importlib\\_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 192 from C header, got 216 from PyObject\n",
      "  return f(*args, **kwds)\n",
      "E:\\anaconda3\\lib\\importlib\\_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 192 from C header, got 216 from PyObject\n",
      "  return f(*args, **kwds)\n"
     ]
    }
   ],
   "source": [
    "############################  糖尿病发病预测 — Logistic建模  ############################\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 768 entries, 0 to 767\n",
      "Data columns (total 10 columns):\n",
      " #   Column                    Non-Null Count  Dtype  \n",
      "---  ------                    --------------  -----  \n",
      " 0   Pregnancies               768 non-null    float64\n",
      " 1   Glucose                   768 non-null    float64\n",
      " 2   BloodPressure             768 non-null    float64\n",
      " 3   SkinThickness             768 non-null    float64\n",
      " 4   Insulin                   768 non-null    float64\n",
      " 5   BMI                       768 non-null    float64\n",
      " 6   DiabetesPedigreeFunction  768 non-null    float64\n",
      " 7   Age                       768 non-null    float64\n",
      " 8   SkinThickness_MissFlag    768 non-null    float64\n",
      " 9   Outcome                   768 non-null    int64  \n",
      "dtypes: float64(9), int64(1)\n",
      "memory usage: 60.1 KB\n"
     ]
    }
   ],
   "source": [
    "#读取特征工程数据\n",
    "trainDatas = pd.read_csv(r'C:\\Users\\HuangSX\\Desktop\\logistic\\Train_datas.csv')\n",
    "trainDatas.head(10)\n",
    "trainDatas.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 输入输出数据分离\n",
    "y_trainDatas = trainDatas['Outcome']\n",
    "X_trainDatas = trainDatas.drop(['Outcome'], axis=1)\n",
    "\n",
    "# 定义特征字段名变量\n",
    "feature_names = X_trainDatas.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "    使用默认参数的Logistics回归模型\n",
    "    正则函数默认使用L2正则\n",
    "\"\"\"\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "lr = LogisticRegression()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "logloss of each fold is： [0.48767012 0.52689324 0.45687743 0.42155531 0.48245162]\n",
      "cv logloss is： 0.4750895435585221\n"
     ]
    }
   ],
   "source": [
    "# 交叉验证评估模型默认参数的Logistics回归模型的性能\n",
    "\"\"\"\n",
    "    采用5折交叉验证\n",
    "\"\"\"\n",
    "from sklearn.model_selection import cross_val_score\n",
    "loss = cross_val_score(lr, X_trainDatas, y_trainDatas, cv=5, scoring='neg_log_loss')\n",
    "\n",
    "# 输出验证的loss\n",
    "print('logloss of each fold is：', -loss)\n",
    "# loss的平均取值\n",
    "print('cv logloss is：', -loss.mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=5, error_score=nan,\n",
       "             estimator=LogisticRegression(C=1.0, class_weight=None, dual=False,\n",
       "                                          fit_intercept=True,\n",
       "                                          intercept_scaling=1, l1_ratio=None,\n",
       "                                          max_iter=100, multi_class='auto',\n",
       "                                          n_jobs=None, penalty='l2',\n",
       "                                          random_state=None, solver='liblinear',\n",
       "                                          tol=0.0001, verbose=0,\n",
       "                                          warm_start=False),\n",
       "             iid='deprecated', n_jobs=-1,\n",
       "             param_grid={'C': [0.1, 1, 10, 100, 1000], 'penalty': ['l1', 'l2']},\n",
       "             pre_dispatch='2*n_jobs', refit=True, return_train_score=False,\n",
       "             scoring='neg_log_loss', verbose=0)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "    使用正则化的Logistic回归，并进行超参数调优\n",
    "\"\"\"\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "\n",
    "# 设置正则函数搜索范围\n",
    "penaltys = ['l1','l2']\n",
    "# 设置正则系数搜索范围\n",
    "Cs = [0.1, 1, 10, 100, 1000]\n",
    "tuned_parameters = dict(penalty = penaltys, C = Cs)\n",
    "\n",
    "# 生成Logistic模型实例\n",
    "lr_penalty = LogisticRegression(solver='liblinear')\n",
    "# 生成GridSearchCV模型调优器\n",
    "grid = GridSearchCV(lr_penalty, tuned_parameters, cv=5, scoring='neg_log_loss', n_jobs=-1)\n",
    "# 调用fit对模型调优\n",
    "grid.fit(X_trainDatas, y_trainDatas)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.47498122043301916\n",
      "{'C': 1, 'penalty': 'l1'}\n"
     ]
    }
   ],
   "source": [
    "# 最佳评估结果\n",
    "print(-grid.best_score_)\n",
    "print(grid.best_params_)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
